*** REPLICATION FILE

*** Authors: Matthew L. Layton (Ohio University) and Amy Erica Smith (Iowa State University and University of Notre Dame)
*** Title: Is It Race, Class, or Gender? The Sources of Perceived Discrimination in Brazil
*** Published: 2017. Latin American Politics and Society.

*** Last Updated 10/1/2016

******************************************
***MAIN ARTICLE ANALYSIS REPLICATION CODE
******************************************

*** DATA: 2010 AmericasBarometer survey in Brazil (Latin American Public Opinion Project, LAPOP)
*** AVAILABLE FOR DOWNLOAD: http://www.lapopsurveys.org

svyset upm [pw=wt], strata(estratopri) /*Set complex survey design elements*/

********************
* VARIABLE CREATION
********************

*DEPENDENT VARIABLE: Perceptions of Discrimination 

*Recoding for frequency analysis /*Used in Appendix Table A1*/
*Racial discrimination
gen dis11r=dis11
recode dis11r (4=0) (3=1) (1=3)
label variable dis11r "Racial Discrimination"

*Class discrimination
gen dis13r=dis13
recode dis13r (4=0) (3=1) (1=3)
label variable dis13r "Class Discrimination"

*Gender discrimination
gen dis12r=dis12
recode dis12r (4=0) (3=1) (1=3)
label variable dis12r "Gender Discrimination"

*0/1 Recoding /*Used in main analyses*/
*Racial discrimination
gen dis11rr=dis11r
recode dis11rr (0=0) (1 2 3=1)
label variable dis11rr "Racial Discrimination"

*Class discrimination
gen dis13rr=dis13r
recode dis13rr (0=0) (1 2 3=1)
label variable dis13rr "Class Discrimination"

*Gender discrimination
gen dis12rr=dis12r
recode dis12rr (0=0) (1 2 3=1)
label variable dis12rr "Gender Discrimination"

*Recoding for experience with any discrimination /*Used in Figures 1 and 2*/
*Any discrimination
egen discrimt=anymatch(dis11rr dis13rr dis12rr),v(1)
replace discrimt=. if dis11rr==. & dis13rr==. & dis12rr==.
label variable discrimt "Any Discrimination"

*Recoding counting types of discrimination experienced /*Used in Appendix Table A6*/
*Discrimination count
egen dis_count=anycount(dis11rr dis13rr dis12rr),v(1)
replace dis_count=. if dis11rr==. & dis13rr==. & dis12rr==.
label variable dis_count "Discrimination count"

*INDEPENDENT VARIABLES: Measures of Race, Class, and Gender

*Race*
*Skin Tone 
gen skintone=colorr
recode skintone (97=.)
replace skintone=(skintone-1)/10
label variable skintone "Skin Tone"

*Self-ID Race /*Used in Appendix Table A2*/
gen etidr=etid
recode etidr (7=6)
label variable etidr "Self-ID Race"
label define etidr 1"White" 2"Multiracial" 3"Indigenous" 4"Black" 5"Asian" 6"Other" 
label values etidr etidr


*Class*
*Quintile of Wealth
set more off
qui gen quintall=.
qui gen wealthscores=.
qui levelsof pais if pais<40, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16 r18 if pais==`k' & ur==1
qui predict  f1`k'u if pais==`k' & ur==1
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1
qui replace wealthscores=f1`k'u if pais==`k' & ur==1

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 r18 if pais==`k' & ur==2
qui predict  f1`k'r if pais==`k' & ur==2
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2
qui replace wealthscores=f1`k'r if pais==`k' & ur==2

}

drop f1*u  f1*r quint*u quint*r wealthscores
label variable quintall " Quintiles of wealth"

tab quintall, g(quintall)

*Level of Education
gen edr =ed
recode  edr (0=0) (1 / 8=1) (9 / 11=2) (12 / 30=3)
capture label drop edr
label  define edr  0"None" 1"Primary" 2"Secondary" 3"Higher"
label  value edr  edr
label variable edr "Education Level"

*Number of Children at Home
gen kids=q12a
replace kids=0 if q12==0
label variable kids "Children at Home"


*Gender*
*Female
gen female=q1-1
label define female 0"Male" 1"Female"
label values female female
label variable female "Female"


*CONTROLS: Included in all models

*Age
gen age=q2
label variable age "Age"

*Change in Household Income
gen q10er=q10e
recode q10er (3=0) (2=1) (1=2)
label variable q10er "Change in Household Income"

*Interpersonal Trust
gen it1r=it1
recode it1r (4=0) (3=1) (1=3)
replace it1r=it1r/3
label variable it1r "Interpersonal Trust"

*Urban
gen urban=ur
recode urban (2=0)
label define urban 0"Rural" 1"Urban"
label values urban urban
label variable urban "Urban"

*Region of Residence
gen estratoprir=estratopri
label variable estratoprir "Region of Residence"

*********
*FIGURE 1
*********
*Reported Racial Discrimination
svy: logit dis11rr
margins, vce(unconditional)
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) ///
	ylabel(,glc(gs14)) title("") horizontal xscale(off) xscale(range(0 .4)) ///
	ytitle("Racial Discrim.", color(black) size(vlarge) orientation(horizontal) width(30) j(right)) ///
	ylabel(,nolab nogrid) xlabel(,format(%9.2f) grid glc(gs14)) xtitle("") saving(RaceDescriptiveGraph,replace)

*Reported Class Discrimination
svy: logit dis13rr
margins, vce(unconditional)
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) ///
	ylabel(,glc(gs14)) title("") horizontal xscale(off) xscale(range(0 .4)) ///
	ytitle("Class Discrim.", color(black) size(vlarge) orientation(horizontal) width(30) j(right)) ///
	ylabel(,nolab nogrid) xlabel(,format(%9.2f) grid glc(gs14)) xtitle("") saving(ClassDescriptiveGraph,replace)

*Reported Gender Discrimination	
svy: logit dis12rr
margins, vce(unconditional)
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) ///
	ylabel(,glc(gs14)) title("") horizontal xscale(off) xscale(range(0 .4)) ///
	ytitle("Gender Discrim.", color(black) size(vlarge) orientation(horizontal) width(30) j(right)) ///
	ylabel(,nolab nogrid) xlabel(,format(%9.2f) grid glc(gs14)) xtitle("") saving(GenderDescriptiveGraph,replace)

*Reported Gender Discrimination over Gender
svy: mean dis12rr, over(female)

*Reported Discrimination (Any)	
svy: logit discrimt
margins, vce(unconditional)
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) ///
	ylabel(,glc(gs14)) title("") horizontal xscale(range(0 .4)) ///
	ytitle("Any Discrim.", color(black) size(vlarge) orientation(horizontal) width(30) j(right)) ///
	ylabel(,nolab nogrid) xlabel(,format(%9.2f) grid glc(gs14)) xtitle("Proportion", size(large)) saving(AnyDiscrimDescriptiveGraph,replace)

*Code to create Figure 1 as found in the paper
graph combine RaceDescriptiveGraph.gph ClassDescriptiveGraph.gph GenderDescriptiveGraph.gph AnyDiscrimDescriptiveGraph.gph, ///
	imargin(zero) xcommon cols(1) graphr(color(white)) title("Proportion Reporting Discrimination Victimization""Brazil, 2010", color(black)) ///
	saving(DiscrimCombineNewExtDescriptiveGraph, replace) note("Source: AmericasBarometer by LAPOP""Note: Prop(Gender Discrimination|Female)=0.14""Ranges are 95% confidence intervals.", color(black))

*********
*FIGURE 2
*********
preserve
foreach var in quintall edr age kids it1r {
	summ `var' 
	replace `var' = (`var'-r(min))/(r(max)-r(min))
}
svy: logit discrimt age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
parmest, format(estimate min95 max95 %8.2f p %8.1e) label norestore
drop if stderr == 0
g num = _n
	summ num
	replace num = (r(max)+1-num)
	sort num
	drop num 
drop if parm == "_cons"
replace label = "Northeast Region" if parm == "1502.estratoprir"
replace label = "Center-West Region" if parm == "1503.estratoprir"
replace label = "Southeast Region" if parm == "1504.estratoprir"
replace label = "South Region" if parm == "1505.estratoprir"
replace label = "Loss of HH Income" if parm == "0.q10er"
replace label = "Increase in HH Income" if parm == "2.q10er"
replace label = "Wealth" if parm == "quintall"
sencode label, replace
graph twoway (scatter label estimate, mcolor(black) msymbol(o)) ///
			(rcap min95 max95 label, horizontal lcolor(black) lwidth(thin)), ///
		title("Determinants of Reporting Any Discrimination", color(black) span) ///
		ytitle("") ylabel(1(1)14, valuelabel angle(horizontal) nogrid) yscale(r(0)) xlabel(, grid) ///
		graphregion(color(white)) legend(off) xline(0, lcolor(black) lwidth(thin)) ///
		note("Source: AmericasBarometer by LAPOP. Logistic regression model." ///
		"All independent variables recoded 0-1. N=2,111. Ranges are 95%" "confidence intervals.", span) ysize(5.5)
restore

*Calculating marginal change in predicted probability for skin tone and number of children at home
svy: logit discrimt age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
margins, dydx(skintone) vce(unconditional)
margins, dydx(kids) vce(unconditional)

*********
*FIGURE 3
*********
*dis11rr by skintone
svy: logit dis11rr i.colorr
margins, at(colorr=(1 2 3 4 5 6 7 8 9 10 11)) vce(unconditional)
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) recast(scatter) ///
	ylabel(,glc(gs14)) title("") ///
	xtitle("Skin Tone", color(black) size(large) width(30) j(right)) ///
	ytitle("Racial", size(large)) name(RaceDiscrimSkinTone,replace)
*dis13rr by skintone
svy: logit dis13rr i.colorr
margins, at(colorr=(1 2 3 4 5 6 7 8 9 10 11)) vce(unconditional)
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) recast(scatter) ///
	ylabel(,glc(gs14)) title("") ///
	xtitle("Skin Tone", color(black) size(large) width(30) j(right)) ///
	ytitle("Class", size(large)) name(ClassDiscrimSkinTone,replace)
*dis12rr by skintone
svy: logit dis12rr i.colorr
margins, at(colorr=(1 2 3 4 5 6 7 8 9 10 11)) vce(unconditional)
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) recast(scatter) ///
	ylabel(,glc(gs14)) title("") ///
	xtitle("Skin Tone", color(black) size(large) width(30) j(right)) ///
	ytitle("Gender", size(large)) name(GenderDiscrimSkinTone,replace)
graph combine RaceDiscrimSkinTone ClassDiscrimSkinTone GenderDiscrimSkinTone, ///
	col(3) title("Probability of Perceiving Discrimination by Skin Tone (1-11)""(Brazil 2010)", color(black) size(huge)) ///
	graphregion(color(white)) xsize(6.5) ysize(2) iscale(*1.8) ycommon  ///
	note("Source: AmericasBarometer by LAPOP. Ranges are 95% confidence intervals.", size(large) span) 

********
*TABLE 1
********

***MODELS 1-3***
svy: logit dis11rr age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr1, title(PRR1)
margins, dydx(skintone) vce(unconditional)
	/* 	margins, at(edr=(0 3)) vce(unconditional) post
		lincom 1._at-2._at   							
		margins, dydx(age) vce(unconditional)
		margins, at(age=(18 78)) vce(unconditional) 	*/

svy: logit dis13rr age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr2, title(PRR2)
margins, dydx(skintone) vce(unconditional)
	/* 	margins, at(quintall=(1 2 3 4 5)) vce(unconditional) post
		lincom 1._at-5._at
		margins, dydx(kids) vce(unconditional)
		margins, dydx(age) vce(unconditional)
		margins, at(age=(18 78)) vce(unconditional)		
		margins, dydx(b1.q10er) vce(unconditional) 		*/

svy: logit dis12rr age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr3, title(PRR3)
margins, dydx(skintone) vce(unconditional)
	/* 	margins, dydx(female) vce(unconditional)
		margins, dydx(age) vce(unconditional)
		margins, at(age=(18 78)) vce(unconditional)		*/

***MODELS 4-6***
svy: logit dis11rr age i.female##c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr4, title(PRR4)
margins, dydx(skintone) at(female=(0 1)) vce(unconditional) post
lincom 1._at-2._at
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) ///
	xline(0, lcolor(black)) ylabel(,nogrid) horizontal recast(scatter) ///
	yscale(range(-.5 1.5)) xscale(range(-.1(.1).5)) xlabel(-.1(.1).5,format(%9.2f) grid glc(gs14)) xscale(off) ytitle("Race") xtitle("") ///
	title("", color(black))  /// 
	saving(Model4_MarginalEffectbyGenderN,replace)

svy: logit dis13rr age i.female##c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr5, title(PRR5)
margins, dydx(skintone) at(female=(0 1)) vce(unconditional) post
lincom 1._at-2._at
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) ///
	xline(0, lcolor(black)) ylabel(,nogrid) horizontal recast(scatter) ///
	yscale(range(-.5 1.5)) xscale(range(-.1(.1).5)) xlabel(-.1(.1).5,format(%9.2f) grid glc(gs14)) xscale(off) ytitle("Class") xtitle("") ///
	title("", color(black)) ///
	saving(Model5_MarginalEffectbyGenderN,replace)

svy: logit dis12rr age i.female##c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr6, title(PRR6)
margins, dydx(skintone) at(female=(0 1)) vce(unconditional) post
lincom 1._at-2._at
marginsplot, plotop(lcolor(black) mcolor(black)) ciop(lcolor(black)) graphr(color(white)) ///
	xline(0, lcolor(black)) ylabel(,nogrid) horizontal recast(scatter) ///
	yscale(range(-.5 1.5)) xscale(range(-.1(.1).5)) xlabel(-.1(.1).5,format(%9.2f) grid glc(gs14)) ytitle("Gender") xtitle("Effects of Skin Tone on Pr(Discrimination)") ///
	title("", color(black)) ///
	saving(Model6_MarginalEffectbyGenderN,replace)

*Export csv values for Table 1
esttab prr1 prr2 prr3 prr4 prr5 prr6, b(%9.3f) se(%9.3f) csv scalars(F N) star	

*********
*FIGURE 4
*********
graph combine Model4_MarginalEffectbyGenderN.gph Model5_MarginalEffectbyGenderN.gph Model6_MarginalEffectbyGenderN.gph, col(1)	graphr(color(white)) ///
	title("Effect of Skin Tone by Gender", color(black)) note("Source: AmericasBarometer by LAPOP. Ranges are 95% confidence intervals.") imargin(zero) ///
	saving(ModelsComb_MarginalEffectbyGenderN,replace)

**********************************
***APPENDIX MODELS
**********************************

*********
*TABLE A1 - Alternate construction of DV as frequency of discrimination, modeled using ologit
*********

*Frequency models (non-interactive)
svy: ologit dis11r age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto a1, title(A1)
margins, dydx(skintone) vce(unconditional)

svy: ologit dis13r age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto a2, title(A2)
margins, dydx(skintone) vce(unconditional)
margins, at(quintall=(1 2 3 4 5)) vce(unconditional)

svy: ologit dis12r age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto a3, title(A3)
margins, dydx(skintone) vce(unconditional)

*Frequency models (interactive: gender*skintone)
svy: ologit dis11r age i.female##c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto a4, title(A4)
margins, dydx(skintone) vce(unconditional)

svy: ologit dis13r age i.female##c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto a5, title(A5)
margins, dydx(skintone) vce(unconditional)
margins, at(quintall=(1 2 3 4 5)) vce(unconditional)

svy: ologit dis12r age i.female##c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto a6, title(A6)
margins, dydx(skintone) vce(unconditional)

esttab a1 a2 a3 a4 a5 a6, b(%9.3f) se(%9.3f) csv scalars(F N) star

*********
*TABLE A2 - Alternate measure of race (self-id race)
*********
*Non-interactive models
svy: logit dis11rr age i.female i.etidr c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr7, title(PRR7)
margins, dydx(4.etidr) vce(unconditional)

svy: logit dis13rr age i.female i.etidr c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr8, title(PRR8)
margins, dydx(4.etidr) vce(unconditional)

svy: logit dis12rr age i.female i.etidr c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr9, title(PRR9)
margins, dydx(4.etidr) vce(unconditional)

*Interactive models
svy: logit dis11rr age i.female##i.etidr c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr10, title(PRR10)
margins, dydx(4.etidr) at(female=(0 1)) vce(unconditional) post
lincom [4.etidr]1._at-[4.etidr]2._at

svy: logit dis13rr age i.female##i.etidr c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr11, title(PRR11)
margins, dydx(4.etidr) at(female=(0 1)) vce(unconditional) post
lincom [4.etidr]1._at-[4.etidr]2._at

svy: logit dis12rr age i.female##i.etidr c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto prr12, title(PRR12)
margins, dydx(4.etidr) at(female=(0 1)) vce(unconditional) post
lincom [4.etidr]1._at-[4.etidr]2._at

esttab prr7 prr8 prr9 prr10 prr11 prr12, b(%9.3f) se(%9.3f) csv scalars(F N) star

*********
*TABLE A3 - Interaction between skin tone & quintile of wealth
*********
svy: logit dis11rr age i.female c.skintone##c.quintall c.edr kids it1r b1.q10er i.urban i.estratoprir
est sto aw1, title(AW1)
margins, dydx(skintone) at(quintall=(1 2 3 4 5)) vce(unconditional)

svy: logit dis13rr age i.female c.skintone##c.quintall c.edr kids it1r b1.q10er i.urban i.estratoprir
est sto aw2, title(AW2)
margins, dydx(skintone) at(quintall=(1 2 3 4 5)) vce(unconditional)

svy: logit dis12rr age i.female c.skintone##c.quintall c.edr kids it1r b1.q10er i.urban i.estratoprir
est sto aw3, title(AW3)
margins, dydx(skintone) at(quintall=(1 2 3 4 5)) vce(unconditional)

esttab aw1 aw2 aw3, b(%9.3f) se(%9.3f) csv scalars(F N) star

*********
*TABLE A4 - Interaction between skin tone & level of education
*********
svy: logit dis11rr age i.female c.skintone##c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto ae1, title(AE1)
margins, dydx(skintone) at(edr=(0 1 2 3)) vce(unconditional)

svy: logit dis13rr age i.female c.skintone##c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto ae2, title(AE2)
margins, dydx(skintone) at(edr=(0 1 2 3)) vce(unconditional)
marginsplot

svy: logit dis12rr age i.female c.skintone##c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto ae3, title(AE3)
margins, dydx(skintone) at(edr=(0 1 2 3)) vce(unconditional)

esttab ae1 ae2 ae3, b(%9.3f) se(%9.3f) csv scalars(F N) star

*********
*TABLE A5 - Interaction between skin tone & number of children at home
*********
svy: logit dis11rr age i.female c.skintone##c.kids c.edr c.quintall it1r b1.q10er i.urban i.estratoprir
est sto ak1, title(AK1)
margins, dydx(skintone) at(kids=(0 1 2 3 4 5 6 7 8 9 10)) vce(unconditional)

svy: logit dis13rr age i.female c.skintone##c.kids c.edr c.quintall it1r b1.q10er i.urban i.estratoprir
est sto ak2, title(AK2)
margins, dydx(skintone) at(kids=(0 1 2 3 4 5 6 7 8 9 10)) vce(unconditional)

svy: logit dis12rr age i.female c.skintone##c.kids c.edr c.quintall it1r b1.q10er i.urban i.estratoprir
est sto ak3, title(AK3)
margins, dydx(skintone) at(kids=(0 1 2 3 4 5 6 7 8 9 10)) vce(unconditional)

esttab ak1 ak2 ak3, b(%9.3f) se(%9.3f) csv scalars(F N) star

*********
*TABLE A6 - Count model of types of discrimination experienced
*********
svy: poisson dis_count age i.female c.skintone c.edr c.quintall kids it1r b1.q10er i.urban i.estratoprir
est sto count1

esttab count1, b(%9.3f) se(%9.3f) csv scalars(F N) star

********************************************************************************************************************
